using UnityEngine;
using System.Collections;
using DanielSig.UnityExtensions;

public class HealthSensor : MonoBehaviour, ISensor
{
	public Rect DisplayRect;
	public TextAnchor DisplayAnchor;
	public Color DisplayColor = Color.red;
	public Color BackColor = Color.black;
	
	public float Health = 1F;
	private float[] _arrayCache = new float[1];
	private Texture2D _tex;
	private Texture2D _back;
	
	public bool HasFixedDirection{ get{ return false; } }
	public SensationType Type { get{ return SensationType.MyHealth; } }
	public float[] Values{ get{ return _arrayCache; } }
	
	void Awake()
	{
		_tex = new Texture2D(1,1);
		_tex.SetPixel(0,0,DisplayColor);
		_tex.Apply();
		
		_back = new Texture2D(1,1);
		_back.SetPixel(0,0,BackColor);
		_back.Apply();
	}
	
	public void UpdateSensors ()
	{
		_arrayCache[0] = Health;
	}
	void ProjectileImpact(float damage)
	{
		if(damage < 0F)
			damage = 0F;
		
		Health -= damage;
		if(Health <= 0)
			SendMessageUpwards("OnDeath");
	}
	void AddHealth(float amount)
	{
		if(amount < 0F)
			amount = 0F;
		
		Health += amount;
		if(Health > 1F)
			Health = 1F;
	}
	void OnGUI()
	{
		Graphics.DrawTexture(DisplayRect.InScreenPixels(DisplayAnchor), _back);
		Graphics.DrawTexture(DisplayRect.ScaleSize(Health, 1F).InScreenPixels(DisplayAnchor), _tex);
	}
}

